<?php

namespace App\Transformers;

use App\Models\Movie;
use League\Fractal\TransformerAbstract;
use Illuminate\Support\Str;
use Carbon\Carbon;
class MovieTransformer extends TransformerAbstract
{
    protected $availableIncludes = ['cover_image', 'watchers', 'watchings', 'watcheds', 'categories', 'actors', 'directors', 'authors', 'player_sources'];
    protected $watchs;
    public function __construct($watchs = null)
    {
        $this->watchs = $watchs;
    }

    public function transform(Movie $movie)
    {
        return [
            'id' => $movie->id,
            'name' => $movie->short_name ?: $movie->name,
            'introduction' => str_replace('<br>', '', $movie->introduction),
            'created_at' => (string) $movie->created_at,
            'updated_at' => (string) $movie->updated_at,
            'published_at' => (array) json_decode($movie->published_at, true),
            'score' => $movie->score,
            'score_people' => $movie->score_people,
            'follower_count' => rand(1,9999),
            'image' => Str::startsWith($movie->cover, 'http') ? $movie->cover : 'https://pic.touxiangge.com/' . $movie->cover,
            'language' => (array) json_decode($movie->language, true),
            'runtime' => $movie->runtime,
            'year' => $movie->year,
            'outher_name' => $movie->outher_name,
            'area' => $movie->area,
            'isWatch' => $this->watch_status($movie),
            'watched_at' => (string) $this->watched_at($movie),
            'watched_count' => $this->watched_count($movie),
            'current_user_watch_status' => $this->current_user_watch_status($movie),
        ];
    }
    protected function is_watch($movie)
    {
        if ($this->watchs && $watch = $this->watchs->where('movie_id', $movie->id)->first()) return $watch;
        else return false;
    }
    protected function watched_at($movie)
    {
        if ($watch = $this->is_watch($movie)) {
            return $watch->updated_at;
        }
        return '';
    }
    protected function watch_status($movie) {
        if ($watch = $this->is_watch($movie)) {
            return $watch->status;
        }
        return false;
    }
    protected function watched_count($movie)
    {
        if ($this->watchs && $watch = $this->watchs->where('movie_id', $movie->id)->first()) return $watch->count;
        else return 0;
    }
    protected function current_user_watch_status($movie)
    {
        if ($this->watchs && $watch = $this->watchs->where('movie_id', $movie->id)->first()) {
            switch($watch->status) {
                case 'watching' :
                    $res = $watch->updated_at->diffForHumans() . '开始打卡';
                break;
                case 'watched' :
                    $res = $watch->updated_at->diffForHumans() . '刷过';
                break;
                case 'inCollect' :
                    $res = $watch->updated_at->diffForHumans() . '加入看单';
                break;
                default:
                    $res = '从未刷过';
            }
            return $res;
        }
        return '从未刷过';
    }
    public function includeWatchers(Movie $movie)
    {
        return $this->collection($movie->bingeWatchers, new UserTransformer());
    }

    public function includeWatchings(Movie $movie)
    {
        $watchings = $movie->watchings()->limit(15)->get();
        return $this->collection($watchings, new UserTransformer());
    }

    public function includeWatcheds(Movie $movie)
    {
        $watcheds = $movie->watcheds()->limit(15)->get();
        return $this->collection($watcheds, new UserTransformer());
    }
    public function includeCategories(Movie $movie)
    {
        return $this->collection($movie->categories, new CategoryTransformer());
    }
    public function includeActors(Movie $movie)
    {
        return $this->collection($movie->actors, new StarTransformer());
    }

    public function includeDirectors(Movie $movie)
    {
        return $this->collection($movie->directors, new StarTransformer());
    }
    public function includeAuthors(Movie $movie)
    {
        return $this->collection($movie->authors, new StarTransformer());
    }
    // 包含播放源数据
    public function includePlayerSources(Movie $movie)
    {
        return $this->collection($movie->player_sources, new PlayerSourceTransformer);
    }
    public function includeCoverImage(Movie $movie)
    {
        if ($movie->cover_image) return $this->item($movie->cover_image, new ImageTransformer);
        else {
            $image = new \App\Models\Image;
            $image->user_id = 0;
            $image->path = '';
            $image->created_at = Carbon::now();
            $image->type = 'movie_cover';
            $image->updated_at = Carbon::now();
        }return $this->item($image, new ImageTransformer);
    }
}
